import {
  editContent,getProjectList,verifyCodeFun
  } from './newsBulletinServ'
  // import { parse } from 'qs'
  import { message } from 'antd'
  // import moment from 'moment'
  import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
  import $ from 'jquery';
  
  const defultState = {
      biz:{
        queryForm: {}
      },
      submitLoading: false,
      submitFeikongLoading: false,
      backLoading: false,
      products:[],
      products: [],
      id:"",
  }
  
  const tmpModule = {
    state: cloneDeep(defultState),
    subscriptions: {
      setup ({dispatch, history}) {
        history.listen(({ pathname, query }) => {
          if('' + tmpModule.pathname === '' + pathname){
            dispatch({type: 'updateStore', payload: cloneDeep(defultState)})
            dispatch({type: 'getProjectList', payload: {}})
          }
        })
      },
    },
  
  
    effects: {
    //获取区域项目
    *getProjectList({payload},{put,select,call}){
      let {areaList} = yield select(state => state[tmpModule.namespace])
      let params = {
          level : 2,
        }
        if(yield call(verifyCodeFun,{})){
          const { data } = yield call(getProjectList, {...params})
          let list = data
          if(!list.length){
            return
          }
          areaList =  list.map((item,index)=>{
            return {
              label: item.buname,
              value: item.buguid,
              children: item.childSysUninessunitList
            }
          })
          yield put({type: 'updateStore', payload: {areaList}})
          yield put({type: 'filterProjectList', payload: {}})
        }
        
      },
      *editContent({payload},{put,select,call}){
        let params = {}
        if(payload.type == "edit"){
          params = {
              id: payload.id,//#修改必填
              ifUse: payload.ifUse,//#选填 如果需要禁用的话 填N
          }
        }
        if(payload.type == "del"){
          params = {
            id: payload.id,//#修改必填
            ifDelete: "Y"//#选填 如果需要逻辑删除的话 填Y
        }
        }
        const { success, msg, data } = yield call(editContent, {...params})
        let defultState = yield select(state => state[tmpModule.namespace])
        if(success){
          if(payload.type == "edit"){
          message.success('切换成功')
          }
          if(payload.type == "del"){
            message.success('删除成功')
            }
          defultState.biz.refreshListDatas(true)
        }
      },
      *filterProjectList({payload},{put,select,call}){    
        let {products,areaList,biz} = yield select(state => state[tmpModule.namespace])
        let list = []
        for(var item of areaList){
          if(biz.queryForm.areaId&&item.value == biz.queryForm.areaId){
            list = item.children
          }
        }
        if(list.length){
          products =  list.map((item,index)=>{
            return {
              label: item.buname,
              value: item.buguid,
            }
          })
        }
        yield put({type: 'updateStore', payload: {products}})
      },
    },
    // 存入状态机
    reducers: {
      updateStore(preState, action) {
        return Object.assign({}, preState, action.payload)
      },
    }
  }
  
  export default tmpModule
  